package eclipse.util.io;
import java.util.*;
import java.io.*;
import java.net.URL;
import eclipse.*;
/**
 *The DataFile class handles all functions that a binary file might use.
 *The DataFile class also symbolizes the DataFile itself as well as a reader for the file.
 *The use of this class is depricated because the PropertyFile class organizes better and the comCode decoder functions better.  
 */
public class DataFile 
{
	private FileWriter 	pFile;
	private Scanner 	InputScan;
	/** 
	 * Reads a String from a binary file.
	 * @param sFile Overwrites the current file for a new file 
	 * */
	public String readLine(String sFile)
	{
		try
		{
			
			InputScan = new Scanner(new FileInputStream(new File(sFile)));
			return readLine();
		}
		catch(Exception e)
		{
			ErrorManager.handleError(e);
		}	
		return "";
	}
	/** Reads a String from a binary file */
	public String readLine()
	{
		try
		{
			return InputScan.nextLine();
		}
		catch(Exception e)
		{
			ErrorManager.handleError(e);
			return "";
		}
	}
	/** 
	 * Reads a double from a binary file
	 * @param sFile Overwrites the current data file with the specified new file  
	 */
	public double readDbl(String sFile)
	{
		try
		{
			InputScan = new Scanner(new FileInputStream(new File(sFile)));
			return readDbl();
		}
		catch(Exception e)
		{
			ErrorManager.handleError(e);
			return 0;
		}	
	}
	/** Reads a double from a binary file */
	public double readDbl()
	{
		return InputScan.nextDouble();
	}
	/** 
	 * Reads a float from the current binary file.
	 * @param sFile Overwrites the current binary file with the specified path of another.
	 */
	public float readFlt(String sFile)
	{
		try
		{
			InputScan = new Scanner(new FileInputStream(new File(sFile)));
			return readFlt();
		}
		catch(Exception e)
		{
			ErrorManager.handleError(e);
			return 0;
		}	
	}
	/** Reads a float from the current binary file */
	public float readFlt()
	{
		return InputScan.nextFloat();
	}
	/** 
	 * Reads a long from the current binary file.
	 * @param sFile Overwrites the current file with the specified new file.
	 */
	public long readLng(String sFile)
	{
		try
		{
			InputScan = new Scanner(new FileInputStream(new File(sFile)));
			return readLng();
		}
		catch(Exception e)
		{
			ErrorManager.handleError(e);
			return 0;
		}	
	}
	/** Reads a long from the current binary file */
	public long readLng()
	{
		return InputScan.nextLong();
	}
	/**
	 * Reads an integer from the current binary file
	 * @param sFile Overwrites the current binary file with the specified.
	 */
	public int readInt(String sFile)
	{
		try
		{
			InputScan = new Scanner(new FileInputStream(new File(sFile)));
			return readInt();
		}
		catch(Exception e)
		{
			ErrorManager.handleError(e);
			return 0;
		}	
	}
	/**Reads an integer from the current binary file */
	public int readInt()
	{
		return InputScan.nextInt();
	}
	/**
	 * Reads a short from the current binary file
	 * @param sFile Overwrites the current binary file with a new file.
	 */
	public short readShort(String sFile)
	{
		try
		{
			InputScan = new Scanner(new FileInputStream(new File(sFile)));
			return readShort();
		}
		catch(Exception e)
		{
			ErrorManager.handleError(e);
			return 0;
		}	
	}
	/**Reads a short from the current binary file */
	public short readShort()
	{
		return InputScan.nextShort();
	}
	/**
	 * Reads a byte from the current binary file.
	 * @param sFile Specifies and loads a new file
	 */
	public byte readByt(String sFile)
	{
		try
		{
			InputScan = new Scanner(new FileInputStream(new File(sFile)));
			return readByte();
		}
		catch(Exception e)
		{
			ErrorManager.handleError(e);
			return 0;
		}	
	}
	/** Reads a byte from the current binary file. */
	public byte readByte()
	{
		return InputScan.nextByte();
	}
	/** 
	 * Writes a short to the current binary file
	 * @param sFile Sets the current binary file
	 * @param sOutput Short value written to the binary file
	 */
	public void writeShort(String sFile, short sOutput)
	{
		loadFile(sFile);
		writeShort(sOutput);
	}
	/**
	 * Writes a short to the current binary file
	 * @param sOutput Short value written to the binary file
	 */
	public void writeShort(short sOutput)
	{
		writeLine(Short.toString(sOutput));
	}
	/**
	 * Writes a double to the current binary file.
	 * @param sFile String path for the binary file.
	 * @param dOutput Double written to the binary file.
	 */
	public void writeDbl(String sFile, double dOutput)
	{
		loadFile(sFile);
		writeDbl(dOutput);
	}
	/**
	 * Writes a double to the current binary file.
	 * @param dOutput Double written to the binary file.
	 */
	public void writeDbl(double dOutput)
	{
		writeLine(Double.toString(dOutput));
	}
	/** 
	 * Writes a float to the current binary file.
	 * @param sFile String path for the binary file.
	 * @param fOutput Float value written to the binary file.
	 */
	public void writeFlt(String sFile, float fOutput)
	{
		loadFile(sFile);
		writeFlt(fOutput);
	}
	/**
	 * Writes a float to the current binary file.
	 * @param fOutput Float value written to the binary file.
	 */
	public void writeFlt(float fOutput)
	{
		writeLine(Float.toString(fOutput));
	}
	/** 
	 * Writes a long to the current binary file.
	 * @param sFile String path of the binary file.
	 * @param lOutput Long value written to the binary file.
	 */
	public void writeLng(String sFile, long lOutput)
	{
		loadFile(sFile);
		writeLng(lOutput);
	}
	/**
	 * Writes a long to the current binary file.
	 * @param lOutput Long value written to the binary file.
	 */
	public void writeLng(long lOutput)
	{
		writeLine(Long.toString(lOutput));
	}
	/**
	 * Writes an integer to the current binary file.
	 * @param sFile String path of the binary file.
	 * @param iOutput Integer written to the binary file.
	 */
	public void writeInt(String sFile, int iOutput)
	{
		loadFile(sFile);
		writeInt(iOutput);
	}
	/**
	 * Writes an integer to the current binary file.
	 * @param iOutput Integer written to the binary file.
	 */
	public void writeInt(int iOutput)
	{
		writeLine(Integer.toString(iOutput));		
	}
	/**
	 * Writes a byte to the current binary file.
	 * @param sFile String path for the binary file.
	 * @param bOutput Byte written to the binary file.
	 */
	public void writeByte(String sFile, byte bOutput)
	{
		loadFile(sFile);
		writeByte(bOutput);
	}
	/**
	 * Writes a byte to the current binary file.
	 * @param bOutput Byte written to the binary file.
	 */
	public void writeByte(byte bOutput)
	{
		writeLine(Byte.toString(bOutput));
	}
	/**
	 * Writes a String to the current binary file.
	 * @param sFile String path to the binary file.
	 * @param sOutput String written to the binary file.
	 */
	public void writeString(String sFile, String sOutput)
	{
		loadFile(sFile);
		writeString(sOutput);
	}
	/**
	 * Writes a String to the current binary file.
	 * @param sOutput String written to the binary file.
	 */
	public void writeString(String sOutput)
	{
		try
		{
			pFile.write(sOutput);
			pFile.flush();
		}
		catch(Exception e)
		{
			ErrorManager.handleError(e);
		}
	}
	/**
	 * Writes a line to the binary file.
	 * @param sFile String path to the binary file.
	 * @param sOutput Line written to the binary file.
	 */
	public void writeLine(String sFile, String sOutput)
	{
		loadFile(sFile);
		writeLine(sOutput);
	}
	/**
	 * Writes a line to the binary file.
	 * @param sOutput Line written to the binary file.
	 */
	public void writeLine(String sOutput)
	{
		try
		{
			pFile.write(sOutput + "\n");
		}
		catch(Exception e)
		{
			ErrorManager.handleError(e);
		}
	}
	/**
	 * Loads a new binary file.
	 * @param uFile URL path to the binary file.
	 */
	public void loadFile(URL uFile)
	{		
		loadFile(uFile.getFile());
	}
	/**
	 * Loads a new binary file.
	 * @param sFile String path to the binary file.
	 */
	public void loadFile(String sFile)
	{		
		try
		{
			pFile = new FileWriter(sFile);
			InputScan = new Scanner(sFile);
		}
		catch(Exception e)
		{
			ErrorManager.handleError(e);
		}
	}
	/**
	 * Closes the binary file.
	 */
	public void closeFile()
	{
		InputScan = null;
		pFile = null;		
	}
	/**
	 * Creates a new DataFile Class with specified Data File path.
	 * @param sFile String path to the DataFile.
	 */
	public DataFile(String sFile)
	{
		loadFile(sFile);
	}
	/**
	 * Creates a new DataFile Class with specified Data File path.
	 * @param uFile URL path to the DataFile.
	 */
	public DataFile(URL uFile)
	{
		loadFile(uFile);
	}
}
